package ihs;

import java.util.ArrayList;

import org.eclipse.jface.text.AbstractDocument;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.ITextSelection;

import org.eclipse.ui.IEditorPart;

import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;

import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

/*
 * Creates an interaction tool that finds lines that were recently edited.
 */
public class IHSTextEditor extends TextEditor implements IDocumentListener{
    
	private AbstractDocument document;
	private ITextSelection textSelection;
	private IEditorPart editorPart;
	private int lastLineEdited = -1;
	private int maxEdits = 10;
	private ArrayList<Integer> lineEdits;
	
	public IHSTextEditor() {
		super();
		lineEdits = new ArrayList<Integer>(maxEdits);
		
		//----------------------------------------------
		
		IWorkbench wb = PlatformUI.getWorkbench();
		IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
		IWorkbenchPage page = win.getActivePage();
		   
		//----------------------------------------------
		
	    editorPart = page.getActiveEditor();
		ITextEditor editor = 
				(ITextEditor)editorPart.getAdapter(ITextEditor.class);
		
		IDocumentProvider provider = editor.getDocumentProvider();
		document = (AbstractDocument) provider.getDocument(editorPart.getEditorInput());
		document.addDocumentListener(this);
	
		// Gets the file history -----------------------------
		/*
		INavigationHistory a = page.getNavigationHistory();
		INavigationLocation[] v = a.getLocations();
		if (v.length > 0)
		{
			for (INavigationLocation str : v)
			{
				//System.out.println(str.getText());
			}
		}
		*/
		// ----------------------------------------------------
	
	}
	public void dispose() {

		super.dispose();
	}
	
	/*
	 * List of last edits.
	 * ArrayList<Integer> of line numbers
	 */
	public ArrayList<Integer> getEdits()
	{
		return lineEdits;
	}
	
	/*
	 * Last line number edited
	 */
	public int getLastLineEdit()
	{
		return lastLineEdited;
	}
	
	public void documentAboutToBeChanged(DocumentEvent event) {
		//System.out.println("Document about to change");
	}
	
	public void documentChanged(DocumentEvent event) {
		textSelection = 
				(ITextSelection) editorPart.getSite().getSelectionProvider().getSelection();

		int offset = textSelection.getOffset();
		try 
		{
			// Gets the line number the cursor is on
			int lineNumber = document.getLineOfOffset(offset);
			lastLineEdited = lineNumber;
			
			if (lineEdits.size() >= maxEdits)
			{
				lineEdits.remove(0);
			}
			lineEdits.add(lineNumber);
			String string = document.get();
			String[] split = string.split("\n");
			
			MarkerFactory.addMarkerToActiveWindow(lineNumber, split[lineNumber], document.getNumberOfLines());
			
			
		} catch (BadLocationException e) {

			e.printStackTrace();
		}
	}
}
